const FLASK_URL = "http://localhost:5000";

type Dataset = {
  id: number;
  name: string;
  dataBalance: number;
  classBalance: number;
  textureBalance: number;
  updateTime: string;
};

type Record = {
  id: number;
  datasetId: number;
  clarity: number;
  contrast: number;
  brightness: number;
  imagePath: string;
  labelPath?: string | null;
};

type ResType = {
  code: number;
  msg: string;
};

const FAKE_DATASETS = [
  {
    id: 1,
    name: "dataset1",
    dataBalance: 20,
    classBalance: 20,
    textureBalance: 20,
    updateTime: "2022-01-01 12:00:00",
  },
  {
    id: 2,
    name: "dataset2",
    dataBalance: 20,
    classBalance: 20,
    textureBalance: 20,
    updateTime: "2022-01-01 12:00:00",
  },
  {
    id: 3,
    name: "dataset3",
    dataBalance: 0.1,
    classBalance: 0.2,
    textureBalance: 0.8,
    updateTime: "2022-01-01 12:00:00",
  },
  {
    id: 4,
    name: "dataset4",
    dataBalance: 0.8,
    classBalance: 0.8,
    textureBalance: 0.8,
    updateTime: "2022-01-01 12:00:00",
  },
  {
    id: 5,
    name: "dataset5",
    dataBalance: 0.8,
    classBalance: 0.8,
    textureBalance: 0.8,
    updateTime: "2022-01-01 12:00:00",
  },
];

const FAKE_RECORDS: Record[] = [
  {
    id: 1,
    datasetId: 1,
    clarity: 20,
    contrast: 20,
    brightness: 20,
    imagePath: "@/assets/Chart_alt.svg",
    labelPath: "https://example.com/label1.jpg",
  },
  {
    id: 2,
    datasetId: 1,
    clarity: 20,
    contrast: 20,
    brightness: 20,
    imagePath: "@/assets/Chart_alt.svg",
    labelPath: "https://example.com/label2.jpg",
  },
  {
    id: 3,
    datasetId: 1,
    clarity: 20,
    contrast: 20,
    brightness: 20,
    imagePath: "@/assets/Chart_alt.svg",
    labelPath: "https://example.com/label2.jpg",
  },
  {
    id: 4,
    datasetId: 1,
    clarity: 20,
    contrast: 20,
    brightness: 20,
    imagePath: "@/assets/Chart_alt.svg",
    labelPath: "https://example.com/label2.jpg",
  },
  {
    id: 5,
    datasetId: 1,
    clarity: 20,
    contrast: 20,
    brightness: 20,
    imagePath: "@/assets/Chart_alt.svg",
    labelPath: "../assets/Chart_alt.svg",
  },
];

const getDatasetList = async (type: string): Promise<Dataset[]> => {
  const map = new Map<string, string>();
  map.set("all", "");
  map.set("original", "?original=1");
  map.set("expanded", "?original=0");
  const res = await fetch(`${FLASK_URL}/dataset${map.get(type)}`);
  const data = await res.json();
  return data.dataList;
};

type addDatasetProps = {
  name: string;
  path: string;
};
const addDataset = async (dataset: addDatasetProps): Promise<ResType> => {
  const res = await fetch(`${FLASK_URL}/dataset`, {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
    },
    body: JSON.stringify(dataset),
  });
  return res.json();
};

const deleteDataset = async (id: number): Promise<ResType> => {
  const res = await fetch(`${FLASK_URL}/dataset`, {
    method: "DELETE",
    headers: {
      "Content-Type": "application/json",
    },
    body: JSON.stringify({ id }),
  });
  return res.json();
};

const getRecordList = async (datasetId: number): Promise<Record[]> => {
  const res = await fetch(`${FLASK_URL}/record?datasetId=${datasetId}`);
  const data = await res.json();
  return data.dataList;
};

const addRecord = async (formData: FormData): Promise<ResType> => {
  const res = await fetch(`${FLASK_URL}/record`, {
    method: "POST",
    body: formData,
  });
  return res.json();
};

const expandDataset = async (datasetId: number): Promise<ResType> => {
  return await new Promise((resolve) => resolve({ code: 0, msg: "增广成功" }));
};

export {
  type Dataset,
  type Record,
  FAKE_RECORDS,
  FAKE_DATASETS,
  FLASK_URL,
  getDatasetList,
  addDataset,
  deleteDataset,
  getRecordList,
  addRecord,
};
